package com.wanfeng.oa.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanfeng.model.system.SysRole;

import com.wanfeng.model.system.SysUserRole;
import com.wanfeng.oa.mapper.SysRoleMapper;
import com.wanfeng.oa.mapper.SysUserRoleMapper;
import com.wanfeng.oa.service.SysRoleService;

import com.wanfeng.vo.system.AssginRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;


    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Map<String,  List> findRoleByUserId(Long userId) {
        HashMap<String, List> roleMap = new HashMap<>();
        //查询所有角色
        List<SysRole> allRolesList = baseMapper.selectList(null);

        //根据用户id查询角色
        List<SysRole> assginRoleList=sysRoleMapper.findRoleByUserId(userId);
        roleMap.put("assginRoleList", assginRoleList);
        roleMap.put("allRolesList", allRolesList);
        return roleMap;
    }

    @Transactional
    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        Long userId = assginRoleVo.getUserId();
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        //userId是唯一的
        //删掉之前的
        //逻辑删除是更新，delete是物理删除
        //updateWrapper
      sysUserRoleMapper.update(null, new LambdaUpdateWrapper<SysUserRole>()
                .set(SysUserRole::getIsDeleted, 1)
                .eq(SysUserRole::getUserId, userId));

        //批量添加
        sysUserRoleMapper.batchAddRoles(userId,roleIdList);

    }
}
